package pingu.interface_graphique;

import java.awt.Font;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.TrueTypeFont;
import org.newdawn.slick.gui.AbstractComponent;
import org.newdawn.slick.gui.ComponentListener;
import org.newdawn.slick.gui.GUIContext;
import org.newdawn.slick.gui.MouseOverArea;
import org.newdawn.slick.gui.TextField;

@SuppressWarnings("deprecation")
public class TextFieldWithImage extends AbstractComponent{
	private Image image;
	private GameContainer container;
	private int x;
	private int y;
	private int width;
	private int height;
	private TextField textField;
	private MouseOverArea mouseOverArea;

	public TextFieldWithImage(GameContainer gc,Image image,int x,int y,int lenghtField,String text) {
		super(gc);
		this.container=gc;
		this.x=x;
		this.y=y;
		int sizeField=5+lenghtField*15;
		
		this.image=image;		
		this.width=image.getWidth()+10+sizeField;
		this.height=image.getHeight();

		Font font=new Font("Verdana",Font.PLAIN,20);
		TrueTypeFont myFont = new TrueTypeFont(font, true);

		textField=new TextField(container, myFont, x+image.getWidth()+10, y+10, sizeField, height-20,
				new ComponentListener() {
			@Override
			public void componentActivated(AbstractComponent source) {
				textField.setText("");
				textField.setFocus(true);
			}
		});
		textField.setText(text);
		textField.setMaxLength(lenghtField);
		
		mouseOverArea=new MouseOverArea(container, null, x, y, width, height);
	}


	public Image getImage(){
		return image;
	}

	public TextField getTextField(){
		return textField;
	}

	public String getText(){
		return textField.getText();
	}


	public void setText(String text){
		textField.setText(text);
	}

	@Override
	public int getHeight() {
		return height;
	}

	@Override
	public int getWidth() {
		return width;
	}

	@Override
	public int getX() {
		return x;
	}

	@Override
	public int getY() {
		return y;
	}

	public void render(Graphics g){
		try {
			render(container, g);
		} catch (SlickException e) {
			// e.printStackTrace();
		}
	}
	
	@Override
	public void render(GUIContext container, Graphics g) throws SlickException {
		image.draw(x, y);
		textField.render(container, g);
		if (mouseOverArea.isMouseOver()) {
			textField.setFocus(true);
		}
	}

	@Override
	public void setLocation(int x, int y) {
		this.x=x;
		this.y=y;
	}

}
